package com.service;

import static com.def.GenericDef.EVT_REWK_IN;
import static com.def.GenericDef.E_BIS_PATH;
import static com.def.GenericDef.E_READ_NOT_FOUND;
import static com.def.GenericDef.E_XPRWKPATH_INVALID_BOX_ID;
import static com.def.GenericDef.E_XPRWKPATH_INVALID_BOX_STATUS;
import static com.def.GenericDef.E_XPRWKPATH_INVALID_PATH_ID;
import static com.def.GenericDef.E_XPRWKPATH_NULL_OPE;
import static com.def.GenericDef.E_XPRWKPATH_NULL_PATH;
import static com.def.GenericDef.E_XPRWKPATH_PRD_EXIST;
import static com.def.GenericDef.E_XPRWKPATH_RTN_OPE_ID;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.def.GenericDef.RUL_ACT_REWORK_OUT;
import static com.def.GenericDef._NORMAL;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dao.IBisDataDao;
import com.dao.IBisOpeDao;
import com.dao.IBisPathDao;
import com.dao.IBisPathiDao;
import com.dao.IRetBoxDao;
import com.dao.IRetPrdInfoDao;
import com.dao.IRetPrdOutDao;
import com.icim.service.ICIMBaseService;
import com.model.Bis_data;
import com.model.Bis_ope;
import com.model.Bis_opeId;
import com.model.Bis_path;
import com.model.Bis_pathId;
import com.model.Bis_pathi;
import com.model.Bis_pathiId;
import com.model.Ret_box;
import com.model.Ret_prd_info;
import com.model.Ret_prd_out;
import com.model.Ret_prd_outId;
import com.rtnBean.NotFoundRtnBeen;
import com.rtnBean.SimpleRtnBeen;
import com.tx.Xprwkpath.XprwkpathI;
import com.tx.Xprwkpath.XprwkpathO;
import com.util.GUIDGenerator;
import com.util.MqOpe;
import com.util.Util;

@Scope("prototype")
@Service("XprwkpathService")
public class XprwkpathService extends ICIMBaseService{
	private Logger logger = Logger.getLogger(XprwkpathService.class);
	@Autowired
	private XprwkpathI xprwkpathI;
	
	@Autowired
	private XprwkpathO xprwkpathO; 
	
	@Autowired
	private IBisPathDao bisPathDao;
	
	@Autowired
	private IBisPathiDao bisPathiDao;

	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;
	
	@Autowired
	private IRetBoxDao retBoxDao;
	
	@Autowired
	private IBisOpeDao bisOpeDao;
	
	@Autowired
	private MqOpe mqOpe;
	
	@Autowired
	private IRetPrdOutDao retPrdOutDao;
	
	@Autowired
	private IBisDataDao bisDataDao;
	
	private List<Ret_prd_info> gRet_prd_infoList = new ArrayList<Ret_prd_info>();
	
	private Bis_pathi gBis_pathi = new Bis_pathi();
	
	private Ret_box gRet_box = new Ret_box();
	
	private Bis_ope gBis_ope = new Bis_ope();
	
	private Timestamp gEvtTimestamp = Util.getCurrentTimestamp();
	
	private String gEvtSeqID = GUIDGenerator.javaGUID();
	
	
	public String subMainProc(String strInMsg){
		String strOutTrx = "";
		logger.info("InTrx is "+ strInMsg );
		Session session = bisPathDao.getCrSession();
		
		xprwkpathO.setTrx_id("XPRWKPATH");
		xprwkpathO.setType_id("O");
		xprwkpathO.setRtn_code(RETURN_CODE_OK);
		xprwkpathO.setRtn_mesg("success");
		try{
			xprwkpathI = (XprwkpathI) mqOpe.inStr2Obj(strInMsg,xprwkpathI);
			char action_flg = xprwkpathI.getAction_flg().charAt(0);
			switch( action_flg ){
				case 'S'   :  skipRwkFunc()  ; break;   
				default                 :  errActionFnc();   
			}
		    if( RETURN_CODE_OK.equals(xprwkpathO.getRtn_code()) ){
		    	session.flush();
			}else{
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		    }		
		}catch(Exception ex){
			xprwkpathO.setRtn_code(RETURN_CODE_UNKNOWN);
			xprwkpathO.setRtn_mesg(ex.toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			//TODO:throw dbRollbackException
		}finally{
			strOutTrx =mqOpe.obj2Str(xprwkpathO); 
			logger.info(strOutTrx);
		}
		return mqOpe.obj2Str(xprwkpathO);
	}
	public void skipRwkFunc() {
		long rtnCode = _NORMAL;
		
		//检查权限
		rtnCode = checkUas();
		if (rtnCode != _NORMAL) {
			return;
		}
		//检查输入数据
		rtnCode = checkInTrx();
		if (rtnCode != _NORMAL) {
			return;
		}
			
		//获得箱子
		rtnCode = queryRetBox();
		if (rtnCode != _NORMAL) {
			return;
		}
		
		//获得该箱子中的所有玻璃
		rtnCode = queryPrdList();
		if (rtnCode != _NORMAL) {
			return;
		}
		
		//如果玻璃当前已经在重工途程，则报错，不让跳转
//		rtnCode = checkCrPathCate();
//		if (rtnCode != _NORMAL) {
//			return;
//		}
		
		//如果产品的CR_PATH和NX_PATH有一个不是MAIN途程，则不允许异转
		rtnCode = checkCrAndNxPathCate();
		if (rtnCode != _NORMAL) {
			return;
		}
		
		//获得重工途程bis_pathi
		rtnCode = queryRwkPath();
		if (rtnCode != _NORMAL) {
			return;
		}
		
		//获得重工线路第一站的站点信息
		rtnCode = queryBisope();
		if (rtnCode != _NORMAL) {
			return;
		}
		
		//检查工艺线路段bis_data DATA_CATE = 'PRST'
		rtnCode = checkBisDataPrst();
		if (rtnCode != _NORMAL) {
			return;
		}
		
		//跳转箱子
		rtnCode = skipRwkBoxInfo();
		if (rtnCode != _NORMAL) {
			return;
		}
		//插入ret_prd_out
		rtnCode = insertRetPrdOut();
		if (rtnCode != _NORMAL) {
			return;
		}
		
		//跳转玻璃
		rtnCode = skipRwkPrdInfo();
		if (rtnCode != _NORMAL) {
			return;
		}
		
		
	}
	
	private long checkUas(){
		String usr_id = xprwkpathI.getEvt_usr();
		if (Util.isSpaceCheck(usr_id)) {
			xprwkpathO.setRtn_code(E_XPRWKPATH_INVALID_BOX_ID+"");
			xprwkpathO.setRtn_mesg("请登录");
			return E_XPRWKPATH_INVALID_BOX_ID;
		}else {
			String checkAdminStr = "SELECT A.EXT_1,A.DATA_EXT,B.EXT_4 FROM " + 
				"(" +
				"(SELECT EXT_1,DATA_EXT FROM BIS_DATA WHERE DATA_CATE ='UFUG'AND data_ext='"+ usr_id +"') AS A " +
				"INNER JOIN " + 
				"( SELECT EXT_1,EXT_4,EXT_3 FROM BIS_DATA WHERE DATA_CATE ='UFGP'AND EXT_4='TOPI'AND EXT_3='Y') AS B " +   
				"ON A.EXT_1 = B.EXT_1" + 
				")";
			String checkFuCoStr = "SELECT C.EXT_1,C.EXT_2,UG.EXT_4 FROM (" + 
					"SELECT A.EXT_1,A.DATA_EXT,B.EXT_4 FROM " + 
					"(" + 
					"(SELECT EXT_1,DATA_EXT FROM BIS_DATA WHERE DATA_CATE ='UFUG'AND data_ext='"+ usr_id +"') " + 
					"AS A " +
					"LEFT JOIN " +  
					"( SELECT EXT_1,EXT_4  FROM BIS_DATA WHERE DATA_CATE ='UFGP'AND EXT_4='TOPI' ) AS B "+   
					"ON A.EXT_1 = B.EXT_1 " + 
					") )AS UG " + 
					"INNER JOIN " +
					"( SELECT EXT_1,EXT_2 FROM  BIS_DATA  WHERE DATA_CATE='UFGC'AND EXT_2='F_FRRW' ) AS C " +  
					"ON UG.EXT_1 = C.EXT_1";
			List<Bis_data> bisDataAdminList = bisDataDao.findBySQL(checkAdminStr);
			
			if (!(bisDataAdminList.isEmpty())) {
				return _NORMAL;
			}else {
				List<Bis_data> bisDataFuCoList = bisDataDao.findBySQL(checkFuCoStr);
				if (bisDataFuCoList.isEmpty()) {
					xprwkpathO.setRtn_code(E_XPRWKPATH_INVALID_BOX_ID+"");
					xprwkpathO.setRtn_mesg("你没有自由重工权限！");
					return E_XPRWKPATH_INVALID_BOX_ID;
				}
			}
		}
		return _NORMAL;
	}
	
	private long checkInTrx(){
		if (Util.isSpaceCheck(xprwkpathI.getBox_id())) {
			xprwkpathO.setRtn_code(E_XPRWKPATH_INVALID_BOX_ID+"");
			xprwkpathO.setRtn_mesg("请输入箱号");
			return E_XPRWKPATH_INVALID_BOX_ID;
		}
		if (Util.isSpaceCheck(xprwkpathI.getFirst_ope_no())) {
			xprwkpathO.setRtn_code(E_XPRWKPATH_INVALID_PATH_ID+"");
			xprwkpathO.setRtn_mesg("请选择重工线路");
			return E_XPRWKPATH_INVALID_PATH_ID;
		}
		if (Util.isSpaceCheck(xprwkpathI.getOut_ope_no_fk())) {
			xprwkpathO.setRtn_code(E_XPRWKPATH_RTN_OPE_ID+"");
			xprwkpathO.setRtn_mesg("请选择返回站点");
			return E_XPRWKPATH_RTN_OPE_ID;
		}
		if (Util.isSpaceCheck(xprwkpathI.getPath_id())) {
			xprwkpathO.setRtn_code(E_XPRWKPATH_INVALID_PATH_ID+"");
			xprwkpathO.setRtn_mesg("请选择重工线路");
			return E_XPRWKPATH_INVALID_PATH_ID;
		}
		if (Util.isSpaceCheck(xprwkpathI.getPath_ver())) {
			xprwkpathO.setRtn_code(E_XPRWKPATH_INVALID_PATH_ID+"");
			xprwkpathO.setRtn_mesg("请选择重工线路");
			return E_XPRWKPATH_INVALID_PATH_ID;
		}
		return _NORMAL;
	}
	
	@SuppressWarnings("unchecked")
	private long queryPrdList(){
		String box_id = xprwkpathI.getBox_id();
		if (Util.isSpaceCheck(box_id)) {
			xprwkpathO.setRtn_code(E_XPRWKPATH_INVALID_BOX_ID+"");
			xprwkpathO.setRtn_mesg("请输入箱号");
			return E_XPRWKPATH_INVALID_BOX_ID;
		}
		String hql = "FROM Ret_prd_info where box_id_fk = '" + box_id + "'";
		gRet_prd_infoList = retPrdInfoDao.find(hql);
		if (gRet_prd_infoList.isEmpty()) {
			xprwkpathO.setRtn_code(E_XPRWKPATH_PRD_EXIST+"");
			xprwkpathO.setRtn_mesg("该箱子中没有玻璃");
			return E_XPRWKPATH_PRD_EXIST;
		}		
		
		return _NORMAL;
	}
	
//	private long checkCrPathCate(){
//		String path_id = gRet_prd_infoList.get(0).getCr_path_id_fk();
//		String path_ver = gRet_prd_infoList.get(0).getCr_path_ver_fk();
//		Bis_pathiId pathiId = new Bis_pathiId();
//		pathiId.setPath_id(path_id);
//		pathiId.setPath_ver(path_ver);
//		
//		Bis_pathi pathi = bisPathiDao.get(pathiId);
//		
//		if (pathi == null) {
//			xprwkpathO.setRtn_code(E_XPRWKPATH_INVALID_PATH_ID+"");
//			xprwkpathO.setRtn_mesg("玻璃所在途程不存在");
//			return E_XPRWKPATH_INVALID_PATH_ID;
//		}
//		
//		if (!("MAIN".equals(pathi.getPath_cate()))) {
//			xprwkpathO.setRtn_code(E_XPRWKPATH_INVALID_PATH_ID+"");
//			xprwkpathO.setRtn_mesg("玻璃当前不在主工艺线路，请先回到主工艺线路");
//			return E_XPRWKPATH_INVALID_PATH_ID;
//		}
//		
//		return _NORMAL;
//	}
	
	private long checkCrAndNxPathCate(){
		/**
		 * 获取CR path信息
		 */
		String cr_path_id = gRet_prd_infoList.get(0).getCr_path_id_fk();
		String cr_path_ver = gRet_prd_infoList.get(0).getCr_path_ver_fk();
		Bis_pathiId crPathiId = new Bis_pathiId();
		crPathiId.setPath_id(cr_path_id);
		crPathiId.setPath_ver(cr_path_ver);
		
		Bis_pathi crPathi = bisPathiDao.get(crPathiId);
		
		if (crPathi == null) {
			xprwkpathO.setRtn_code(E_XPRWKPATH_INVALID_PATH_ID+"");
			xprwkpathO.setRtn_mesg("玻璃的当前途程["+ cr_path_id +"]不存在");
			return E_XPRWKPATH_INVALID_PATH_ID;
		}
		
//		if (!("MAIN".equals(crPathi.getPath_cate()))) {
//			xprwkpathO.setRtn_code(E_XPRWKPATH_INVALID_PATH_ID+"");
//			xprwkpathO.setRtn_mesg("玻璃当前不在主工艺线路，请先出账后再转入重工线路。");
//			return E_XPRWKPATH_INVALID_PATH_ID;
//		}
		/**
		 * 获取Nx path信息
		 */
		String nx_path_id = gRet_prd_infoList.get(0).getNx_path_id_fk();
		String nx_path_ver = gRet_prd_infoList.get(0).getNx_path_ver_fk();
		Bis_pathiId nxPathiId = new Bis_pathiId();
		nxPathiId.setPath_id(nx_path_id);
		nxPathiId.setPath_ver(nx_path_ver);
		
		Bis_pathi nxPathi = bisPathiDao.get(nxPathiId);
		
		if (nxPathi == null) {
			xprwkpathO.setRtn_code(E_XPRWKPATH_INVALID_PATH_ID+"");
			xprwkpathO.setRtn_mesg("玻璃下一途程["+ nx_path_id +"]不存在");
			return E_XPRWKPATH_INVALID_PATH_ID;
		}
		
		if (!("MAIN".equals(nxPathi.getPath_cate()))) {
			xprwkpathO.setRtn_code(E_XPRWKPATH_INVALID_PATH_ID+"");
			xprwkpathO.setRtn_mesg("玻璃已转入重工线路，无法再次转入，请先回到主工艺线路");
			return E_XPRWKPATH_INVALID_PATH_ID;
		}
		return _NORMAL;
	}
	private long queryRwkPath(){
		String path_id = xprwkpathI.getPath_id();
		String path_ver = xprwkpathI.getPath_ver();
		
		Bis_pathiId pathiId = new Bis_pathiId();
		pathiId.setPath_id(path_id);
		pathiId.setPath_ver(path_ver);
		
		gBis_pathi = bisPathiDao.get(pathiId);
		
		if (gBis_pathi == null) {
			xprwkpathO.setRtn_code(E_XPRWKPATH_INVALID_PATH_ID+"");
			xprwkpathO.setRtn_mesg("该重工途程不存在");
			return E_XPRWKPATH_INVALID_PATH_ID;
		}
		return _NORMAL;
	}
	
	private long queryRetBox(){
		String box_id = xprwkpathI.getBox_id();
		gRet_box = retBoxDao.get(box_id);
		if (gRet_box == null) {
			xprwkpathO.setRtn_code(E_XPRWKPATH_INVALID_BOX_ID+"");
			xprwkpathO.setRtn_mesg("该箱号不存在");
			return E_XPRWKPATH_INVALID_BOX_ID;
		}
		if (!("WAIT".equals(gRet_box.getBox_stat()))) {
			xprwkpathO.setRtn_code(E_XPRWKPATH_INVALID_BOX_STATUS+"");
			xprwkpathO.setRtn_mesg("只有WAIT状态的箱子才能跳转重工");
			return E_XPRWKPATH_INVALID_BOX_STATUS;
		}
		return _NORMAL;
	}
	
	private long queryBisope(){
		String ope_id = xprwkpathI.getFirst_ope_no();
		if (Util.isSpaceCheck(ope_id)) {
			xprwkpathO.setRtn_code(E_XPRWKPATH_NULL_PATH+"");
			xprwkpathO.setRtn_mesg("该重工线路维护有误，缺少开始站点");
			return E_XPRWKPATH_NULL_PATH;
		}
		
		Bis_opeId bis_opeId = new Bis_opeId();
		bis_opeId.setOpe_id(ope_id);
		bis_opeId.setOpe_ver("00000");
		
		gBis_ope = bisOpeDao.get(bis_opeId);
		if (gBis_ope == null) {
			xprwkpathO.setRtn_code(E_XPRWKPATH_NULL_OPE+"");
			xprwkpathO.setRtn_mesg("该站点["+ope_id+"]没有维护");
			return E_XPRWKPATH_NULL_OPE;
		}
		
		return _NORMAL;
	}
	
	private long checkBisDataPrst(){
		String box_proc_id = gRet_box.getNx_proc_id_fk();
		if (Util.isSpaceCheck(box_proc_id)) {
			xprwkpathO.setRtn_code(E_XPRWKPATH_NULL_OPE+"");
			xprwkpathO.setRtn_mesg("该箱子["+gRet_box.getBox_id()+"]nx_proc_id为空");
			return E_XPRWKPATH_NULL_OPE;
		}
		String first_ope_no = xprwkpathI.getFirst_ope_no();
		
		Bis_opeId bis_opeId = new Bis_opeId();
		Bis_ope bis_ope = new Bis_ope();
		
		bis_opeId.setOpe_id(first_ope_no);
		bis_opeId.setOpe_ver("00000");
		
		bis_ope = bisOpeDao.get(bis_opeId);
		
		if (Util.isSpaceCheck(bis_ope.getProc_id())) {
			xprwkpathO.setRtn_code(E_XPRWKPATH_NULL_OPE+"");
			xprwkpathO.setRtn_mesg("该重工线路首站点维护有误");
			return E_XPRWKPATH_NULL_OPE;
		}
		
		String hql1 = "FROM Bis_data WHERE DATA_CATE = 'PRST' AND DATA_ID = '" + bis_ope.getProc_id() + "'";
		String hql2 = "FROM Bis_data WHERE DATA_CATE = 'PRST' AND DATA_ID = '" + box_proc_id + "'";
		
		List<Bis_data> bis_datas1 = new ArrayList<Bis_data>();
		List<Bis_data> bis_datas2 = new ArrayList<Bis_data>();
		
		bis_datas1 = bisDataDao.find(hql1);
		bis_datas2 = bisDataDao.find(hql2);
		
		if (bis_datas1.isEmpty()) {
			xprwkpathO.setRtn_code(E_XPRWKPATH_NULL_OPE+"");
			xprwkpathO.setRtn_mesg("该站点["+bis_ope.getProc_id() + "]在bis_data中没有维护，DATA_CATE = 'PRST'");
			return E_XPRWKPATH_NULL_OPE;
		}
		if (bis_datas2.isEmpty()) {
			xprwkpathO.setRtn_code(E_XPRWKPATH_NULL_OPE+"");
			xprwkpathO.setRtn_mesg("该站点["+box_proc_id + "]在bis_data中没有维护，DATA_CATE = 'PRST'");
			return E_XPRWKPATH_NULL_OPE;
		}
		
		String ext_4_1 = bis_datas1.get(0).getExt_4();
		String ext_4_2 = bis_datas2.get(0).getExt_4();
//		取消2500自由重工的跨工段限制
//		if (ext_4_1.equals(ext_4_2)) {
//			return _NORMAL;
//		}else {
//			xprwkpathO.setRtn_code(E_XPRWKPATH_NULL_OPE+"");
//			xprwkpathO.setRtn_mesg("不能跨工艺段自由重工");
//			return E_XPRWKPATH_NULL_OPE;
//		}
		return _NORMAL;
	}
	
	private long skipRwkBoxInfo(){
		String nx_path_id_fk = xprwkpathI.getPath_id();
		String nx_path_ver_fk = xprwkpathI.getPath_ver();
		String nx_ope_no_fk = xprwkpathI.getFirst_ope_no();
		String nx_ope_id_fk = xprwkpathI.getFirst_ope_no();
		String nx_ope_ver_fk = "00000";
		String nx_proc_id_fk = gBis_ope.getProc_id();
		String nx_toolg_id_fk = gBis_ope.getToolg_id();
		
		Ret_box box = new Ret_box();
		box = gRet_box;
		box.setBox_stat("WAIT");
		box.setNx_ope_id_fk(nx_ope_id_fk);
		box.setNx_ope_no_fk(nx_ope_no_fk);
		box.setNx_ope_ver_fk(nx_ope_ver_fk);
		box.setNx_path_id_fk(nx_path_id_fk);
		box.setNx_path_ver_fk(nx_path_ver_fk);
		box.setNx_proc_id_fk(nx_proc_id_fk);
		box.setNx_toolg_id_fk(nx_toolg_id_fk);
		
		box.setEvt_cate(EVT_REWK_IN);
		box.setEvt_usr(xprwkpathI.getEvt_usr());
		box.setEvt_timestamp(gEvtTimestamp);
		
		retBoxDao.update(box);
		return _NORMAL;
	}
	
	private long skipRwkPrdInfo(){
		for (Ret_prd_info prd : gRet_prd_infoList) {
			String nx_path_id_fk = xprwkpathI.getPath_id();
			String nx_path_ver_fk = xprwkpathI.getPath_ver();
			String nx_ope_no_fk = xprwkpathI.getFirst_ope_no();
			String nx_ope_id_fk = xprwkpathI.getFirst_ope_no();
			String nx_ope_ver_fk = "00000";
			String nx_proc_id_fk = gBis_ope.getProc_id();
			String nx_toolg_id_fk = gBis_ope.getToolg_id();
			
			prd.setNx_ope_id_fk(nx_ope_id_fk);
			prd.setNx_ope_no_fk(nx_ope_no_fk);
			prd.setNx_ope_ver_fk(nx_ope_ver_fk);
			prd.setNx_path_id_fk(nx_path_id_fk);
			prd.setNx_path_ver_fk(nx_path_ver_fk);
			prd.setNx_proc_id_fk(nx_proc_id_fk);
			prd.setNx_toolg_id_fk(nx_toolg_id_fk);
			
			prd.setEvt_cate(EVT_REWK_IN);
			prd.setEvt_usr(xprwkpathI.getEvt_usr());
			prd.setEvt_timestamp(gEvtTimestamp);
			prd.setEvt_seq_id(gEvtSeqID);
			
			retPrdInfoDao.update(prd);
		}
		return _NORMAL;
	}
	
	private long insertRetPrdOut(){
		for (Ret_prd_info prd : gRet_prd_infoList) {
			Ret_prd_out ret_prd_out = new Ret_prd_out();
			Ret_prd_outId ret_prd_outId = new Ret_prd_outId();

			ret_prd_outId.setPrd_seq_id_fk(prd.getPrd_seq_id());
			ret_prd_outId.setEvt_seq_id_fk(gEvtSeqID);
			ret_prd_outId.setEvt_timestamp(gEvtTimestamp);

			ret_prd_out.setId(ret_prd_outId);
			ret_prd_out.setRul_act_code(RUL_ACT_REWORK_OUT);

			// From (Bis_path_swh)
			ret_prd_out.setFr_path_id_fk(prd.getCr_path_id_fk());
			ret_prd_out.setFr_path_ver_fk(prd.getCr_path_ver_fk());
			ret_prd_out.setFr_ope_no_fk(prd.getCr_ope_no_fk());
			ret_prd_out.setSwh_id("M");
			ret_prd_out.setSwh_typ("R");

			// To (Bis_path)
			ret_prd_out.setTo_path_id_fk(xprwkpathI.getPath_id());
			ret_prd_out.setTo_path_ver_fk(xprwkpathI.getPath_ver());
			ret_prd_out.setTo_ope_no_fk(xprwkpathI.getFirst_ope_no());

			// Out
			ret_prd_out.setOut_path_id_fk(prd.getNx_path_id_fk());
			ret_prd_out.setOut_path_ver_fk(prd.getNx_path_ver_fk());
			ret_prd_out.setOut_ope_no_fk(xprwkpathI.getOut_ope_no_fk());
			//判断cr途程为rewk
//			Bis_pathiId pathiId = new Bis_pathiId();
//			pathiId.setPath_id(prd.getCr_path_id_fk());
//			pathiId.setPath_ver(prd.getCr_path_ver_fk());
//			
//			Bis_pathi bis_pathi = bisPathiDao.get(pathiId);
//		    if(!"MAIN".equals(bis_pathi.getPath_cate())){
//				ret_prd_out.setOut_path_id_fk(prd.getNx_path_id_fk());
//				ret_prd_out.setOut_path_ver_fk(prd.getNx_path_ver_fk());
//		    }	
		
			//check exist
			Bis_pathId checkBisPathId = new Bis_pathId();
			Bis_path checkBisPath = new Bis_path();
			checkBisPathId.setPath_id_fk(ret_prd_out.getOut_path_id_fk());
			checkBisPathId.setPath_ver_fk(ret_prd_out.getOut_path_ver_fk());
			checkBisPathId.setCr_ope_no(xprwkpathI.getOut_ope_no_fk());
			checkBisPath = bisPathDao.get(checkBisPathId);
			if (null == checkBisPath) {
				xprwkpathO.setRtn_code(E_XPRWKPATH_NULL_OPE+"");
				xprwkpathO.setRtn_mesg("返回站点["+xprwkpathI.getOut_ope_id_fk() + "]返回路线["+ret_prd_out.getOut_path_id_fk()+"]"
						+ "返回路线版本["+ret_prd_out.getOut_path_ver_fk()+"]在bis_path中不存在");
				return E_XPRWKPATH_NULL_OPE;	
			}


			retPrdOutDao.save(ret_prd_out);
		}
		
		return _NORMAL;
	}
	
	public void errActionFnc(){
		xprwkpathO.setTrx_id("XPRWKPATH");
		xprwkpathO.setType_id("O");
		xprwkpathO.setRtn_code("0000009");
		xprwkpathO.setRtn_mesg("Invalid Action_flg");
	}
	
}
